package com.suifeng.card.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.suifeng.card.api.common.Result;
import com.suifeng.card.entity.Product;
import com.suifeng.card.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * (Product)表控制层
 *
 */
@RestController
@Slf4j
public class ProductController {
    /**
     * 服务对象
     */
    @Resource
    private ProductService productService;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/products/{id}")
    public Result<Product> queryById(@PathVariable("id") Integer id) {
        try {
            if (log.isInfoEnabled()) {
                log.info("根据产品id查询产品入参{}", JSON.toJSONString(id));
            }
            Preconditions.checkArgument(!Objects.isNull(id), "查询产品id不能为空");
            return Result.ok(this.productService.queryById(id));
        } catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            log.error("根据产品id查询产品异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 获取所有产品
     * @return
     */
    @GetMapping("/products")
    public Result<Product> getAll() {
        try {
            //enable=1
            return Result.ok(this.productService.getAll());
        } catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            log.error("获取所有产品异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 新增数据
     *
     * @param product 实体
     * @return 新增结果
     */
    @PostMapping("/products")
    public Result<Product> add(@RequestBody Product product) {
        try {
            if (log.isInfoEnabled()) {
                log.info("新增产品入参{}", JSON.toJSONString(product));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(product.getName()), "名称不能为空");
            Preconditions.checkArgument(!Objects.isNull(product.getDuration()), "名称不能为空");
            Preconditions.checkArgument(!Objects.isNull(product.getOriginalprice()), "原价不能为空");
            Preconditions.checkArgument(!Objects.isNull(product.getPrice()), "现价不能为空");
            Preconditions.checkArgument(!Objects.isNull(product.getMonthlyprice()), "月卡价格不能为空");
            return Result.ok(this.productService.insert(product));
        } catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            log.error("新增产品异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }

    }

    /**
     * 编辑数据
     *
     * @param product 实体
     * @return 编辑结果
     */
    @PatchMapping("/products/{id}")
    public Result<Boolean> edit(@RequestBody Product product) {

        try {
            if (log.isInfoEnabled()) {
                log.info("新增产品入参{}", JSON.toJSONString(product));
            }
            Preconditions.checkArgument(!Objects.isNull(product.getId()), "产品不能为空");
            return Result.ok(this.productService.update(product));
        } catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            log.error("编辑产品异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }
}

